<template>
  <div class="poem-card">
    <van-nav-bar
      title="古诗词学习"
      left-arrow
      @click-left="$emit('close')"
    />
    
    <div class="card-content" v-if="effectiveCardData">
      <div v-html="processedContent"></div>
    </div>
    
    <div class="loading" v-else>
      <van-loading type="spinner" color="#6e8efb" size="24px">生成诗词卡片中...</van-loading>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue';
import { useRoute } from 'vue-router';

const props = defineProps({
  cardData: {
    type: Object,
    default: null
  }
});

const route = useRoute();
const localCardData = ref(null);

// 从 localStorage 获取卡片数据
onMounted(() => {
  try {
    const storedCard = localStorage.getItem('currentCard');
    if (storedCard) {
      localCardData.value = JSON.parse(storedCard);
      console.log('从 localStorage 获取的卡片数据:', localCardData.value);
    }
  } catch (error) {
    console.error('获取卡片数据失败:', error);
  }
});

// 使用 props 或 localStorage 中的数据
const effectiveCardData = computed(() => {
  return props.cardData || localCardData.value;
});

defineEmits(['close']);

// 处理HTML内容
const processedContent = computed(() => {
  if (!effectiveCardData.value?.content) return '';
  
  // 如果内容是markdown代码块格式，提取HTML内容
  const match = effectiveCardData.value.content.match(/```html\n([\s\S]*?)```/);
  if (match) {
    return match[1];
  }
  
  return effectiveCardData.value.content;
});

// 添加全局样式
const style = document.createElement('style');
style.textContent = `
  .poem-card {
    min-height: 100vh;
    background-color: #faf6f1;
    font-family: "SimSun", "STSong", serif;
  }
  
  .card-content {
    padding: 16px;
    max-width: 800px;
    margin: 0 auto;
  }
  
  .card-content :deep(h1) {
    font-size: 28px;
    color: #2c3e50;
    margin-bottom: 12px;
    text-align: center;
    font-weight: bold;
  }
  
  .card-content :deep(h2) {
    font-size: 18px;
    color: #666;
    margin-bottom: 24px;
    text-align: center;
    font-weight: normal;
  }
  
  .card-content :deep(section) {
    background: rgba(255, 255, 255, 0.9);
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
    border: 1px solid #e8e8e8;
  }
  
  .card-content :deep(section h3) {
    font-size: 18px;
    color: #8b4513;
    margin-bottom: 16px;
    font-weight: bold;
    border-bottom: 1px solid #e8e8e8;
    padding-bottom: 8px;
  }
  
  .card-content :deep(section p) {
    font-size: 16px;
    color: #333;
    line-height: 1.8;
    margin-bottom: 12px;
    text-align: justify;
  }
  
  .card-content :deep(section ul) {
    padding-left: 20px;
    margin-bottom: 12px;
  }
  
  .card-content :deep(section li) {
    font-size: 15px;
    color: #333;
    margin-bottom: 8px;
    line-height: 1.6;
  }
  
  .card-content :deep(.poem-text) {
    font-size: 18px;
    line-height: 2;
    text-align: center;
    margin: 20px 0;
    color: #2c3e50;
  }
  
  .card-content :deep(.annotation) {
    color: #666;
    font-size: 14px;
  }
  
  .card-content :deep(.highlight) {
    color: #8b4513;
    font-weight: bold;
  }
  
  .loading {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 200px;
  }
  
  @media (max-width: 768px) {
    .card-content {
      padding: 12px;
    }
    
    .card-content :deep(h1) {
      font-size: 24px;
    }
    
    .card-content :deep(h2) {
      font-size: 16px;
    }
    
    .card-content :deep(section h3) {
      font-size: 16px;
    }
    
    .card-content :deep(section p),
    .card-content :deep(section li) {
      font-size: 14px;
    }
    
    .card-content :deep(.poem-text) {
      font-size: 16px;
    }
  }
`;

onMounted(() => {
  document.head.appendChild(style);
});
</script> 